package org.ssi.web.tags.html;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts2.views.jsp.TagUtils;
import org.ssi.web.struts.bean.Pager;

import com.bjtech.web.util.Constants;

public class PaginationTag extends TagSupport {
	private static final long serialVersionUID = 1030108214578921520L;

	protected String name= null;

	protected String property = "pager";

	protected String scope= null;
	
	protected String method= null;
	
	protected String showsize= "true";
	
	protected int type= 0;
	
	protected int size = 3;

	public int getSize() {
		return size;
	}

	public String getShowsize() {
		return showsize;
	}

	public void setShowsize(String showsize) {
		this.showsize = showsize;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public PaginationTag() {}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public int doStartTag() throws JspException {

		Object value = TagUtils.getStack(pageContext).findValue(property);
		if (value == null)
			return SKIP_BODY;
		
		Pager pager = (Pager)value;
		StringBuffer output = new StringBuffer();
		switch(type){
			case 0:standard(output,pager);break;
			case 1:AltaVista(output,pager);break;
			case 2:web1(output,pager);break;
			case 3:web1_en(output,pager);break;
			case 4:indexWeb(output,pager);break;
		}
		try
		{

			pageContext.getOut().print(output.toString());
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	private void indexWeb(StringBuffer output,Pager pager){
		output.append("共 "+pager.getAllcol()+" 条信息 ");
		output.append("<a href=\"javascript:skip('1');\">首页</a> ");
		output.append("<a href=\"javascript:skip('"+pager.getPrevious()+"');\">上一页</a> ");
		output.append("<a href=\"javascript:skip('"+pager.getNext()+"');\">下一页</a> ");
		output.append("<a href=\"javascript:skip('"+pager.getAllpage()+"');\">尾页</a> ");
	//	output.append("页次："+pager.getCpage()+"/"+pager.getAllpage()+"页 "+pager.getPagesize()+"篇/页 转到第");
		
		output.append("\n<input name=\""+property+".cpage\" type=\"hidden\" value=\""+pager.getCpage()+"\" size=\"2\" maxlength=\"4\">");
		//output.append("\n<input type=\"submit\" value=\"跳转\"");
		//if(method!=null){
		//	output.append(" onclick=\"javascript:document."+name+".elements['method'].value= '"+method+"';\"");
		//	output.append(" onclick=\"javascript:m = document."+name+".action.indexOf('!');");
		//	output.append("e = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
		//	output.append("if(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;");
		//}
		//output.append(">");
		
		output.append("\n\n<script language=\"javascript\">");
		output.append("\nfunction skip(page){");
		if(method!=null){
			//output.append("\n\tdocument."+name+".elements['method'].value= '"+method+"';");
			output.append("\n\tm = document."+name+".action.indexOf('!');");
			output.append("\n\te = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("\n\tif(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;");
		}
		output.append("\n\tdocument."+name+".elements['"+property+".cpage'].value= page;");
		output.append("\n\tdocument."+name+".submit();");
		output.append("\n}");
		output.append("\n</script>\n");
	}
	private void web1(StringBuffer output,Pager pager){
		output.append("共 "+pager.getAllcol()+" 条信息 ");
		output.append("<a href=\"javascript:skip('1');\">首页</a> ");
		output.append("<a href=\"javascript:skip('"+pager.getPrevious()+"');\">上一页</a> ");
		output.append("<a href=\"javascript:skip('"+pager.getNext()+"');\">下一页</a> ");
		output.append("<a href=\"javascript:skip('"+pager.getAllpage()+"');\">尾页</a> ");
		output.append("页次："+pager.getCpage()+"/"+pager.getAllpage()+"页 "+pager.getPagesize()+"篇/页 转到第");
		
		output.append("\n<input name=\""+property+".cpage\" type=\"text\" value=\""+pager.getCpage()+"\" size=\"2\" maxlength=\"4\">");
		output.append("\n<input type=\"submit\" value=\"跳转\"");
		if(method!=null){
			//output.append(" onclick=\"javascript:document."+name+".elements['method'].value= '"+method+"';\"");
			output.append(" onclick=\"javascript:m = document."+name+".action.indexOf('!');");
			output.append("e = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("if(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;\"");
		}
		output.append(">");
		
		output.append("\n\n<script language=\"javascript\">");
		output.append("\nfunction skip(page){");
		if(method!=null){
			//output.append("\n\tdocument."+name+".elements['method'].value= '"+method+"';");
			output.append("\n\tm = document."+name+".action.indexOf('!');");
			output.append("\n\te = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("\n\tif(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;");
		}
		output.append("\n\tdocument."+name+".elements['"+property+".cpage'].value= page;");
		output.append("\n\tdocument."+name+".submit();");
		output.append("\n}");
		output.append("\n</script>\n");
	}
	
	private void web1_en(StringBuffer output,Pager pager){
		output.append("Total "+pager.getAllpage()+" Pages "+pager.getAllcol()+" Records ");
		output.append("[<a href=\"javascript:skip('1');\">First Page</a>] ");
		output.append("[<a href=\"javascript:skip('"+pager.getPrevious()+"');\">Previous Page</a>] ");
		output.append("[<a href=\"javascript:skip('"+pager.getNext()+"');\">Next Page</a>] ");
		output.append("[<a href=\"javascript:skip('"+pager.getAllpage()+"');\">Last Page</a>] ");
		//output.append("页次："+pager.getCpage()+"/"+pager.getAllpage()+"页 "+pager.getPagesize()+"篇/页 转到第");
		
		output.append("\n<input name=\""+property+".cpage\" type=\"text\" value=\""+pager.getCpage()+"\" size=\"2\" maxlength=\"4\">");
		output.append("\n<input type=\"submit\" value=\"Go\"");
		if(method!=null){
			//output.append(" onclick=\"javascript:document."+name+".elements['method'].value= '"+method+"';\"");
			output.append(" onclick=\"javascript:m = document."+name+".action.indexOf('!');");
			output.append("e = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("if(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;\"");
		}
		output.append(">");
		
		output.append("\n\n<script language=\"javascript\">");
		output.append("\nfunction skip(page){");
		if(method!=null){
			//output.append("\n\tdocument."+name+".elements['method'].value= '"+method+"';");
			output.append("\n\tm = document."+name+".action.indexOf('!');");
			output.append("\n\te = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("\n\tif(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;");
		}
		output.append("\n\tdocument."+name+".elements['"+property+".cpage'].value= page;");
		output.append("\n\tdocument."+name+".submit();");
		output.append("\n}");
		output.append("\n</script>\n");
	}
	
	private void standard(StringBuffer output,Pager pager){
		output.append("\n共&nbsp;"+pager.getAllpage()+"&nbsp;页&nbsp;共&nbsp;"+pager.getAllcol()+"&nbsp;条记录 ");
		output.append("\n<A href=\"javascript:skip('1');\">[首&nbsp;&nbsp;页]</A> ");
		output.append("\n<A HREF=\"javascript:skip('"+pager.getPrevious()+"');\">[上一页]</A> ");
		output.append("\n<A HREF=\"javascript:skip('"+pager.getNext()+"');\">[下一页]</A> ");
		output.append("\n<A HREF=\"javascript:skip('"+pager.getAllpage()+"');\">[末&nbsp;&nbsp;页]</A> ");
		output.append("\n当前第	 <input name=\""+property+".cpage\" type=\"text\" class='sysipt' value=\""+pager.getCpage()+"\" size=\"2\" maxlength=\"4\"> 页 ");
		if(showsize!=null && showsize.equalsIgnoreCase("true")){
			output.append("\n每页<input name=\"pager.pagesize\" type=\"text\" class='sysipt' value=\""+pager.getPagesize()+"\" size=\"2\" maxlength=\"4\">条 ");
		}
		output.append("\n<input type=\"submit\" class='syssub' value=\"跳转\"");
		if(method!=null){
			//output.append(" onclick=\"javascript:document."+name+".elements['method'].value= '"+method+"';\"");
			output.append(" onclick=\"javascript:m = document."+name+".action.indexOf('!');");
			output.append("e = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("if(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;\"");
		}
		output.append(">");
		
		output.append("\n\n<script language=\"javascript\">");
		output.append("\nfunction skip(page){");
		if(method!=null){
			//output.append("\n\tdocument."+name+".elements['method'].value= '"+method+"';");
			output.append("\n\tm = document."+name+".action.indexOf('!');");
			output.append("\n\te = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("\n\tif(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;");
		}
		output.append("\n\tdocument."+name+".elements['"+property+".cpage'].value= page;");
		output.append("\n\tdocument."+name+".submit();");
		output.append("\n}");
		output.append("\n</script>\n");
	}
	
	private void AltaVista(StringBuffer output,Pager pager){
		if(pager.getCpage()>1){
			output.append("\n<A HREF=\"javascript:skip('"+pager.getPrevious()+"');\"><img src=\""+Constants.ContextPath+"/images/per.gif\" border=\"0\" align=\"absmiddle\" /></A> ");
		}
		int i = pager.getCpage()-size;
		i = i<1?1:i;
		for(;i<pager.getCpage();i++){
			output.append("\n&nbsp;<A HREF=\"javascript:skip('"+i+"');\"><u>"+i+"</u></A> ");
		}
		
		output.append("\n&nbsp;<font color=\"#ff0000\">"+pager.getCpage()+"</font>");
		
		i = pager.getCpage()+1;
		for(;i<=pager.getCpage()+size && i<=pager.getAllpage();i++){
			output.append("\n&nbsp;<A HREF=\"javascript:skip('"+i+"');\"><u>"+i+"</u></A> ");
		}
		
		if(pager.getCpage()<pager.getAllpage()){
			output.append("\n&nbsp;<A HREF=\"javascript:skip('"+pager.getNext()+"');\"><img src=\""+Constants.ContextPath+"/images/next.gif\" border=\"0\" align=\"absmiddle\" /></A> ");
		}
		
		output.append("\n<input name=\""+property+".cpage\" type=\"hidden\" value=\""+pager.getCpage()+"\">");
		
		output.append("\n\n<script language=\"javascript\">");
		output.append("\nfunction skip(page){");
		if(method!=null){
			//output.append("\n\tdocument."+name+".elements['method'].value= '"+method+"';");
			output.append("\n\tm = document."+name+".action.indexOf('!');");
			output.append("\n\te = document."+name+".action.substring(document."+name+".action.lastIndexOf('.'));");
			output.append("\n\tif(m != -1) document."+name+".action = document."+name+".action.substring(0, m) + '!' + '"+method+"' + e;");
		}
		output.append("\n\tdocument."+name+".elements['"+property+".cpage'].value= page;");
		output.append("\n\tdocument."+name+".submit();");
		output.append("\n}");
		output.append("\n</script>\n");
	}

	public void release() {
		super.release();
		name = null;
		property = "pager";
		scope = null;
		method = null;
		showsize = null;
		type = 0;
		size = 3;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

}
